package cn.tutu.blockchain.exchange.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.common.Page;
import cn.tutu.blockchain.exchange.dao.ExchangeDao;
import cn.tutu.blockchain.exchange.entity.Exchange;
import cn.tutu.blockchain.exchange.service.ExchangeService;
import cn.tutu.blockchain.rate.dao.RateDao;
import cn.tutu.blockchain.rate.entity.Rate;
import cn.tutu.blockchain.utils.ArithUtils;

@Service
@Transactional
public class ExchangeServiceImpl implements ExchangeService {

	@Autowired
	private ExchangeDao exchangeDao;

	@Autowired
	private RateDao rateDao;

	@Override
	public void create(Exchange exchange) {
		Rate rate = rateDao.findNewRate();
		exchange.setRate(rate.getCkt() + ":" + rate.getRmb());
		exchange.setRmb(ArithUtils.mul(Double.parseDouble(rate.getRmb()), exchange.getCurrencyValue(), 2));
		exchangeDao.insert(exchange);

	}

	@Override
	public void modifyStatus(Exchange exchange) {
		exchangeDao.updateStatus(exchange);
	}

	@Override
	public PageInfo<Exchange> getByUserId(String userId, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Exchange> list = exchangeDao.findByUserId(userId);
		PageInfo<Exchange> pageInfo = new PageInfo<Exchange>(list);
		return pageInfo;
	}

	@Override
	public List<Exchange> getByStatus(String status) {
		return exchangeDao.getByStatus(status);
	}

	@Override
	public Exchange getOneByStatus(String code) {
		// TODO Auto-generated method stub
		return exchangeDao.findOneByStatus(code);
	}

	@Override
	public void modifyLastDate(String id) {
		exchangeDao.updateLastDate(id);

	}

}
